package com.xyb.service;

import com.xyb.config.exception.MyException;
import com.xyb.dao.SysMenuRoleDao;
import com.xyb.dao.SysRoleDao;
import com.xyb.dao.SysUserRoleDao;
import com.xyb.entity.StatusCode;
import com.xyb.entity.SysMenuRole;
import com.xyb.entity.SysRole;
import com.xyb.utils.IdUtils;
import com.xyb.vo.SysRoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleService {
    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysMenuRoleDao sysMenuRoleDao;
    @Resource
    private SysUserRoleDao sysUserRoleDao;
    @Resource
    private IdUtils idUtils;


    public List<SysRole> selectSysRoleList() {
        List<SysRole> sysRoleList = sysRoleDao.selectSysRoleList();
        ;
        return sysRoleList;
    }

    public SysRoleVo selectSysRoleByRoleId(Long roleId) {
        SysRole sysRole = sysRoleDao.selectSysRoleByRoleId(roleId);
        List<SysMenuRole> sysMenuRoleList = sysMenuRoleDao.selectSysMenuRoleByRoleId(roleId);
        List<Long> menuIds = new ArrayList<>();
        sysMenuRoleList.forEach(i -> {
            menuIds.add(i.getMenuId());
        });
        SysRoleVo sysRoleVo = SysRoleVo.builder()
                .roleId(roleId)
                .roleName(sysRole.getRoleName())
                .roleDesc(sysRole.getRoleDesc())
                .showOrder(sysRole.getShowOrder())
                .status(sysRole.getStatus())
                .menuIds(menuIds)
                .build();
        return sysRoleVo;
    }

    public String insertSysRole(SysRoleVo vo) {
        SysRole sysRole = SysRole.builder()
                .roleId(idUtils.nextId())
                .roleName(vo.getRoleName())
                .roleDesc(vo.getRoleDesc())
                .status(1)
                .build();
        sysRoleDao.insertSysRole(sysRole);
        return "success";
    }

    /**
     * 修改用户角色
     *
     * @param vo
     * @return
     */
    public String updateSysRole(SysRoleVo vo) {
        //角色信息
        SysRole sysRole = SysRole.builder()
                .roleId(vo.getRoleId())
                .roleName(vo.getRoleName())
                .roleDesc(vo.getRoleDesc())
                .build();
        sysRoleDao.updateSysRoleByRoleId(sysRole);
        //管理员不可修改
        if (vo.getRoleId().intValue() == 0) {
            return "success";
        }
        //删除角色菜单表相关
        sysMenuRoleDao.deleteSysMenuByRoleId(vo.getRoleId());
        if(vo.getMenuIds()!=null&&vo.getMenuIds().size()>0){
            //添加角色菜单表相关
            List<Long> menuIds = vo.getMenuIds();
            List<SysMenuRole> list = new ArrayList<>();
            menuIds.forEach(i -> {
                SysMenuRole sysMenuRole = SysMenuRole.builder()
                        .menuRoleId(idUtils.nextId())
                        .menuId(i)
                        .roleId(vo.getRoleId())
                        .build();
                list.add(sysMenuRole);
            });
            sysMenuRoleDao.insertSysMenuRoleBySysMenuRoleList(list);
        }
        return "success";
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    public String deleteSysRoleByRoleId(Long roleId) {
        //管理员不可删除
        if(roleId.intValue()==0){
            throw new MyException(StatusCode.ADMIN_REFUSED_EXCEPTION);
        }
        //删除角色
        sysRoleDao.deleteSysRoleByRoleId(roleId);
        //删除菜单角色
        sysMenuRoleDao.deleteSysMenuByRoleId(roleId);
        //删除用户角色
        sysUserRoleDao.deleteSysUserByRoleId(roleId);
        return "success";
    }

    public String updateSysRoleShowOrder(Long[] roleIds) {
        List<SysRole> list = new ArrayList<>();
        for (int i = 0; i < roleIds.length; i++) {
            SysRole sysRole = new SysRole();
            sysRole.setRoleId(roleIds[i]);
            sysRole.setShowOrder((long)i);
            list.add(sysRole);
        }
        sysRoleDao.updateSysRoleBySysRoleList(list);
        return "success";
    }
}








































